const app = getApp();
import { msgList } from "../../api/news";
import { year, month, day } from "../../utils/util"
import { getStorage } from "../../utils/localStorage"
import { baseUrl } from "../../api/request"
Component({
  data: {
    navData: {
      title: '消息', //导航栏名称
      level: 1 //导航栏级别  
    },
    baseUrl: baseUrl,
    user: {},
    list: [],
    triggered:false,
    listQuery: {
      userId: null,
      pageSize: 10,
      pageNum: 1,
    }, //列表查询条件
    total: 0,
    pageTop: app.globalData.navHeight + app.globalData.navTop,
    pageBottom: app.globalData.bottomLift - 12,
  },
  lifetimes: {
    attached() {
      if (app.loginState()) return;
      this.setData({
        user: getStorage("user"),
        "listQuery.userId": getStorage("user").id
      });
      this.getMsgList();
    },
    detached() { },
  },
  pageLifetimes: {
    show: function () {
      let newsId = Number(app.globalData.newsId), newsFlag = app.globalData.newsFlag,
        index = this.data.list.findIndex(item => item.id === newsId),
        value = "list[" + index + "].flag";
      if (newsId) {
        this.setData({
          [value]: newsFlag
        });
      };
    }
  },
  methods: {
    //获取系统推送消息
    getMsgList() {
      msgList(this.data.listQuery).then(res => {
        if (res.data.code === 200) {
          this.setData({
            triggered:false,
            list: res.data.data,
          });
          // console.log(this.data.list, 'listnews');
        };
      });
    },
    //列表点击事件
    listClick(e) {
      let item = e.currentTarget.dataset.item;
      wx.navigateTo({
        url: '/pagesXiaoXi/pages/MessageDetails/index?id=' + item.id,
      })
    },
    /* 事件处理按钮点击事件 */
    eventClick() {
      wx.navigateTo({
        url: '/pagesXiaoXi/pages/event/index',
      });
    },
  },
})